
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import dao.AccountDAO;

import pojo.Account;
import util.MessageParser;
import util.UserParser;

/**
 * Servlet implementation class Hello
 */

public class AccountController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ArrayList<Account> listAccount ;
	private ArrayList<Account> listAccountOnline ;
	private AccountDAO accountDAO ;
	private Gson gson ;
	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
		gson = new Gson();
		// dummy arraylist
		/*listAccount = new ArrayList<Account>();
		
		Account acc = new Account();
		acc.setId(1);
		acc.setUsername("admin");
		acc.setPassword("admin");
		Account acc1 = new Account();
		acc1.setId(2);
		acc1.setUsername("user1");
		acc1.setPassword("user1");
		Account acc2 = new Account();
		acc2.setId(2);
		acc2.setUsername("user2");
		acc2.setPassword("user2");
		listAccount.add(acc);
		listAccount.add(acc1);
		listAccount.add(acc2);*/
		String connectionString = getServletContext().getInitParameter("ConnectionString");
		String username = getServletContext().getInitParameter("Username");
		String password = getServletContext().getInitParameter("Password");
		accountDAO = new AccountDAO(connectionString, username, password);
		listAccount = accountDAO.getListUser();
		listAccountOnline = new ArrayList<Account>();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String pathInfo = request.getPathInfo();
		UserParser parser = new UserParser(pathInfo);
		String name = parser.getName();
		String jsonObject ;
		if (name != null) {
			jsonObject = gson.toJson(accountDAO.getUser(name));
		} else {
			 jsonObject = gson.toJson(listAccountOnline);		
		}
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		out.write(jsonObject);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// response.setHeader("Access-Control-Allow-Origin","*");
		PrintWriter out = response.getWriter();
		BufferedReader in = request.getReader();
		Boolean online = false;

		
		Account account = gson.fromJson(in, Account.class);
		for (Account acc : listAccountOnline) {
			if(acc.getUsername().equals(account.getUsername())){
				online = true;
				break;
			}
		}
		if(account.getPassword() == null){ // xử lý khi user log out
			System.out.println("Client "+ account.getUsername() +" has left");
			for (Account acc : listAccountOnline) {
				if(acc.getUsername().equals(account.getUsername())){
					listAccountOnline.remove(acc);
					break;
				}
			}
		} else { // xử lý khi user log in
			if(!online){
				for (Account acc : listAccount) {
					if (acc.getUsername().equals(account.getUsername())) {
						if (acc.getPassword().equals(account.getPassword())) {
							String jsonObject = gson.toJson(acc);
							listAccountOnline.add(acc);
							response.setContentType("application/json");
							out.write(jsonObject);
							return;
						}	
					}	
				}
				response.setStatus(601);		
			} else {
				response.setStatus(600);
			}
			
		}
			
		
		

	}

}
